<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <form action="user/login" method="post">
    <input type="text" name="userName" id="userName" placeholder="请输入用户名" />
    <br>
    <input type="password" name="passWord" id="passWord" placeholder="请输入密码" /><br />
    <input type="button" value="登录" οnclick="login()" id="submit" />
  </form>

  <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.4/jquery.min.js"></script>

  <script>
    //DOM加载完毕之后执行
    $(function () {
      //提交按钮 绑定点击事件
      $("#submit").click(function () {
        //点击执行回调函数
        //获取输入框的值
        var userName = $("#userName").val()
        var passWord = $("#passWord").val()

        //判断是否为空 去除前后空格 中间不去除
        if (userName.trim() == "" || passWord.trim() == "") {
          alert("请输入用户名或者密码")
          return
        }

        //异步执行HTTP请求 （Ajax）请求
        $.ajax({
          url: "user/login",
          data: { "username": userName, "password": passWord },
          type: "POST",
          dataType: 'json',
          //响应成功之后 返回的数据
          success: function (data) {
            console.log(data)
            if (data.code == 1) {
              alert("登录成功")
              window.location.href = "file.html"
            } else {
              alert("登录失败")
              $("#userName").val("")
              $("#password").val("")
            }
          }
        })

      })
    })




  </script>


</body>

</html>